--[[ Copyright 2012, Chris Ho                               ]]--
--[[ MIT License (Please read the LICENSE file for details) ]]--

Use("common/Const");
Use("common/Log");
Use("common/Position");
Use("common/Task");
Use("common/Creature");
Use("common/Pet");
Use("common/Skill");

-- The fencer persona overrides some of the default persona behavior
-- for the fencer's combat ability. It will focus on dealing with
-- aggressive creatures for all friendly creatures.
local name = "Fencer";
local Initialize, status = loadfile(PERSONALITY_DEFAULT);
if (Initialize == nil) then
  Alert(name .. ": " .. status);
  return ME;
end

Fencer = {};

--[[ Override Default Rating Calculation ]]--
-- Fencers treat everyone equally. They prefer to target aggressive
-- cratures.
function Fencer.RateTarget(self, _id)
  local target = GetV(V_TARGET, _id);
  local pettarget = GetV(V_TARGET, self.id);
  local ownertarget = GetV(V_TARGET, self.owner.id);
  local score = Pet.RateTarget(self, _id);

  -- If he's attacking me, give bonus.
  if (target == self.id) then score = score + 5 / self:GetTranquility() end
  -- If he's attacking the owner, give bonus.
  if (target == self.owner.id) then score = score + 5 / self:GetTranquility() end
  -- If he's my current target, give bonus.
  if (pettarget == _id) then score = score + 2 / self:GetTranquility() end
  -- If he's my owner's target, give bonus.
  if (ownertarget == _id) then score = score + 2 / self:GetTranquility() end

  -- Now check against friendly creatures.
  local key, value;
  for key, value in ipairs(self.friends) do
    local friendtarget = GetV(V_TARGET, key);
    if (target == key) then score = score + 5 / self:GetTranquility() end
    if (friendtarget == _id) then score = score + 2 / self:GetTranquility() end
  end

  return score;
end

--[[ Determine if a target is a threat ]]--
-- Fencers have a more aggressive friend-foe evaluation. They include the
-- current owner's intended target (whether the owner is under attack or
-- not) as well as targets of friends.
function Fencer.Consider(self, _id)
  -- Skip our allies in the search.
  if (self:FetchFriend(_id) == nil) then
    local intent = GetV(V_TARGET, _id);
    if ((self:FetchFriend(intent) ~= nil) or (self:FindAssistants(_id).n > 0)) then
      -- This actor has targeted an ally, or he is a target of an ally.
      if (IsMonster(_id) == 1) then
        -- Object is a threat (hostile monster).
        Debug(name .. ": " .. _id .. " Monster Threat");
        return true;
      else
        -- Object is not a monster.
        local motion = GetV(V_MOTION, _id);
        if ((motion == MOTION_ATTACK) or (motion == MOTION_ATTACK2)) then
          -- But the object is still attacking us, which makes it a threat.
          Debug(name .. ": " .. _id .. " Object Threat");
          return true;
        end
      end
    end
  end

  -- Not a threat.
  return false;
end



-- Initialize base personality.
--local instance = Initialize(...);
--local instance = Initialize(arg[1]);
local instance = Initialize();

instance.RateTarget = Fencer.RateTarget;
instance.Consider = Fencer.Consider;

Info(name .. " Loaded");
return instance;
